/**
 * @author benny.zheng
 * @data 2016-06-06
 * @description 本文件用于方便复制粘贴UI模块之用，请更新这里的说明
 *              另外，考虑到一般是放在js/src/pages/page-name/main.js，因此使用../../
 *              如果不是这个目录，请更改成正确的相对路径
 */
//----------------require--------------

var viewport = require("mlib/dom/viewport"); // viewport
var base = require("mlib/comp/base"); // 基础对象
var parseModule = require("mlib/dom/parseModule"); // 页面模块自动解析
var wechat = require("../plugin/wx/wx-api");

var round = require("../plugin/util/round");
var date = require("../plugin/util/date");

var ajax = require("mlib/io/ajax");
var closest = require("mlib/dom/closest");
var Alert = require("../layer/weuialert");
var weuiMessage = require('../layer/weuimessage');
var touch = require("mlib/evt/touch");
var virtualLink = require("mlib/util/virtualLink");
var follow = require("../public/js/follow");
var parseUrl = require("../plugin/url/parse-url");

var listRender = require("./list.ejs");
var nullRender = require("./empty.ejs");




module.exports = function(node, opts) {
    //-----------声明模块全局变量-------------
    var nodeList = null; // 存储所有关键节点
    var that = base();
    var data = null;

    var listelem = null;
    var receiveUrl = null;
    var urlParams = null;
	
    //-------------事件响应声明---------------
    var evtFuncs = {
        getVoucher:function(evt){
            follow(null,true);
        },
			receiveShelf:function(evt){
				  if(this.classList.contains("disabled")){
				  	return;
				  }
				  var node = closest(evt.target,"button.receive-btn"),
					  cardId = node.dataset.cardid;
					  custFuncs.receiveReq(cardId);
			},
			batchReceive:function(evt){
				  if(this.classList.contains("disabled")){
				  	return;
				  }
				custFuncs.receiveReq(0);
			}
	}

    //-------------子模块实例化---------------
    var initMod = function() { 
		urlParams =  parseUrl();
        listelem = nodeList.listelem;
        custFuncs.getShelf();
        
		virtualLink(function(evt){
			location.href = evt.target.getAttribute("data-href");
		});

    }

    //-------------绑定事件------------------
    var bindEvents = function() {
                touch.on(document.body, 'tap', "button.receive-btn", evtFuncs.receiveShelf);
                touch.on(nodeList.batchReceive, 'tap', evtFuncs.batchReceive);
                touch.on(document.body, 'tap', ".followme", evtFuncs.getVoucher);
	}

    //-------------自定义函数----------------
    var custFuncs = {
			getMpinfo:function(result){		
				ajax({ 
					url: "/index.php?r=CouponApi/data/wechat",
					onSuccess: function(res) {
						wechat('open',{
							"title":"发现一大波优惠券，快来领取啦~",
							"link":location.origin+data.shareUrl,
							"imgUrl":res.data.imgDomain+"/weixin/couponLog.jpg",
							"desc":"【"+res.data.publicname+"】优惠券，好东西要一起分享~",
							"success":function (res) {
								urlParams.shareCode = "shelf";
								ajax({
									url: "/index.php?r=CouponApi/data/statistics",
									data:urlParams, 
									method: "post",
									onSuccess: function(result) {
									},
									onError: function(xmlHttp) {
									}
								});
							}
						}).init();

					},
					onError: function(xmlHttp) {
						 that.unLock();
					}
				});
				
			},
    		receiveReq:function(cardid,requrl){
					ajax({
						url: receiveUrl,
						data: {"cardId":cardid}, 
						method: "post",
						onSuccess: function(result) {
		                    if(result.status>0){
							    Alert("领取成功",{"okFn":function(){
							    	if(cardid!=0){
							    		location.href=result.data[0].detailRouter;
							    	}else{
										 nodeList.batchReceive.classList.add("disabled");
							    	}							    	
							    }});
		                    }else{
		                    	Alert(result.msg);
		                    }
						},
						onError: function(xmlHttp) {
						   that.unLock();
						}
					});
    		},
           getShelf:function(){

				ajax({
					url:data.requestAPIUrl,
					data: {}, 
					method: "post",
					onSuccess: function(result) {
						
						that.unLock();
						receiveUrl = result.data.receiveUrl;
						if(result.data.cardList.length==0){
							document.body.innerHTML = nullRender();
						}else{
							listelem.insertAdjacentHTML('beforeend',listRender({"codelist":result.data.cardList}));	
							if(result.data.canBatch==1){
                               nodeList.btngroup.style.visibility = "visible"; 
                               nodeList.footer.parentElement.removeChild(nodeList.footer); 
							}else{
								nodeList.footer.style.display = 'block';
                                nodeList.btngroup.parentElement.removeChild(nodeList.btngroup);                                 
							}
							if(result.data.canShare==1){
                               custFuncs.getMpinfo();
							}else{
                                wechat('off').init();
							}							
						}					
						 				
					},
					onError: function(xmlHttp) {
					   that.unLock();
					}
				});

           }
    }

    //-------------一切从这开始--------------
    var init = function(_data) {

        data = _data;

        // 找到所有带有node-name的节点
        nodeList = parseModule(node);
        // 子模块实例化
        initMod();
        // 绑定事件
        bindEvents();


    }

    //---------------暴露API----------------
    that.init = init;

    return that;
};